<template>
	<view class="address-wrapper">
		<view v-if="addressList.length > 0">
			<view class="address-cell-box p30 bg-color-white mt20 pb20" style="padding-bottom: 0;"
				:class="item.id == selectedAddressId ? 'selected-address-cell-box' : ''"
				v-for="(item,index) in addressList" :key="index" @click="onChooseAddress(item)">
				<view class="flex_lr_m bb1 pb30">
					<view>
						<view class="flex_m b" style="font-weight: 800;">
							<view class="f30 c333">
								{{ item.name }}
							</view>
							<view class="f30 c333 ml35">
								{{ item.phone }}
							</view>
							<!-- <view class="ml20" style=" width: 100rpx">
		              <van-tag wx:if='{{ item.isDefault == 1 }}' round type="success">默认</van-tag>
		            </view> -->
						</view>
						<view class="f28 c333 ell_more ell_2 mt10">
							{{ item.address }}
						</view>
					</view>
				</view>
				<view class="">
					<view class="flex add-opt">
						<view class="f28" @click="onUpdateAddress(item)">
							<image src="../../static/images/Edit.png" mode="aspectFill"></image>
							编辑
						</view>
						<view class="f28" @click='onDeleteAddress(item)'>
							<image src="../../static/images/Delete.png" mode="aspectFill"></image>
							删除
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="flex_line_c no_msg_box" v-else>
			<image class="no_msg" src="../../static/images/no_data.png"></image>
			<view class="f28 c666">您还没有收货地址</view>
		</view>
		<view class="fixed-button flex_m bt1">
			<navigator url='/pages2/address/addAddress' class="bttom-btn">新增收货地址</navigator>
		</view>
	</view>
</template>

<script>
	import {
		qzApi
	} from '@/config/request.js';
	export default {
		data() {
			return {
				addressList: [],
				selectedAddressId: "",
				busnessType: ""

			}
		},
		methods: {
			async getAddresslist() {
				try {
					const data = await qzApi.addressList();
					this.addressList = data.data ? data.data : []
				} catch (error) {
					// 处理错误
					console.error('获取用户信息失败:', error);
				}
			},


			onChooseAddress(e) {
				
				if (this.busnessType == 'select') {
					this.selectedAddressId = e.id
					var pages = getCurrentPages()
					var prevPage = pages[pages.length - 2];
					prevPage.$vm.getValue(e.id)
					// this.$emit("addId",e.id)
					// var pages = getCurrentPages()
					// var prevPage = pages[pages.length - 2];
					// prevPage.addressId = e.id
					uni.navigateBack({
						delta: 1
					})
				}
			},

			//编辑收货地址
			onUpdateAddress(data) {
				var id = data.id
				uni.navigateTo({
					url: '/pages2/address/addAddress?addressId=' + id
				})
			},
			//删除收货地址
			onDeleteAddress(data) {
				let that = this;
				const id = data.id
				uni.showModal({
					content: '确定要删除当前地址?',
					success: (res) => {
						if (res.confirm) {
							that.deleteAddressRequest(id)
						} else if (res.cancel) {}
					}
				})
			},
			async deleteAddressRequest(id) {
				if (id) {
					try {
						const data = await qzApi.deleteAddress(id);
						if (data.code == 0) {
							this.getAddresslist()
						} else {
							uni.showToast({
								icon: 'none',
								title: data.msg
							})
						}
					} catch (error) {
						console.log('getAddressList', error);
					} finally {
						uni.hideLoading()
					}
				}
			}
		},
		onShow() {
			this.getAddresslist()
		},
		onLoad(options) {
			if (options.busnessType) {
				this.busnessType = options.busnessType;
				this.selectedAddressId = options.busnessType == 'select' ? options.selectedAddressId : ''
			}
			uni.setNavigationBarTitle({
				title: options.busnessType == 'select' ? '选择收货地址' : '我的地址'
			})
		},

	}
</script>

<style lang="scss" scoped>
	page{
		background: #F4F5F9;
	}
	
	.add-opt{
		width: 100%;
		view{
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #666;
			border-right: 1rpx solid #E6E6E6;
			padding: 20rpx 0;
			image{
				width: 28rpx;
				height: 28rpx;
				margin-right: 10rpx;
			}
		}
		view:last-child{
			border-right: none;
		}
	}
	.bb1{
		border-bottom: 1rpx solid #E6E6E6;
	}
	.address-cell-box {
		border-radius: 20rpx;
		margin: 20rpx 20rpx;
	}

	.selected-address-cell-box {
		border: 1px solid #F06454;
	}
</style>